clear all;close all;clc
% x_loc = 0:-5:-355;
x_loc = [0:-5:-175 180:-5:5];
y_loc = [-57,-30,-15,0,15,30,45,60,75];

L_or_R = 1;
soft_hard = 0; % this is the hard mask!!!
rad = 0.3;
if_mask=0;

for Condition = 1:4

    if Condition==1
        tar_index = 13; % target is on the left, -60
        noise_index = 61; % noise on the right, +60; we only need to run the model for +60 to get rid of noise!
        f1=figure('position',[100 100 750 220]);
    elseif Condition==2
        tar_index = 4; % target is on the left, -15
        noise_index = 70; % noise on the right, +15; we only need to run the model for +60 to get rid of noise!
        f1=figure('position',[100 100 750 550]);
    elseif Condition==3
        tar_index = 1; % target is on the front center 0,
        noise_index = 72; % noise on the right, +5;
        plot_start=5;
    elseif Condition==4
        tar_index = 56; % target is on the right +85,
        noise_index = 55; % noise on the right, +90;  Very bad, but still need to do it and grade.
        % elseif Condition==5
        %     % % % % % % % tar_index = 58; % target is on the right +75,
        %     % % % % % % % noise_index = 52; % noise on the right, +105;
        %     tar_index = 60; % target is on the right +65,
        %     noise_index = 55; % noise on the right, +90;
        % elseif Condition==6
        %     tar_index = 37; % target is on the back +180,
        %     noise_index = 38; % noise on the back, +175;
    end


    Elevation = 4; % ear level, 4
    x_loc(tar_index)
    x_loc(noise_index)
    pause(0.5)

    fs = 32.1e3; % this is the one for the 3D3A database
    out_hart = ncread('Acoustic/Subject1/Subject1_HRIRs.sofa',"Data.IR");
    igroup = 1
    target = [];
    noise = [];
    randNoise = randperm(25);
    for isent = 1:25
        if isent<10
            [ori,fs2]=audioread(['Speech_sound/BEL_SR/BEL_SR0' num2str(igroup) '0' num2str(isent) '.wav']);
        else
            [ori,fs2]=audioread(['Speech_sound/BEL_SR/BEL_SR0' num2str(igroup) num2str(isent) '.wav']);
        end
        target = [target;resample(ori,fs,fs2)];
        if randNoise(isent)<10
            [BB,fs2]=audioread(['Speech_sound/BEL_SR/BEL_SR0' num2str(igroup) '0' num2str(randNoise(isent)) '.wav']);
        else
            [BB,fs2]=audioread(['Speech_sound/BEL_SR/BEL_SR0' num2str(igroup) num2str(randNoise(isent)) '.wav']);
        end
        noise = [noise; BB];
    end
    noise = [noise; BB];
    noise_reversed = flipud(noise);

    % add spatial properties
    %1))) target
    Index = (Elevation - 1)*72+tar_index;%
    h_l = squeeze(out_hart(:,1,Index)); % Left HRIRs
    h_r = squeeze(out_hart(:,2,Index)); % Right HRIRs
    new_tar(:,1) = conv(target,h_l,'same');
    new_tar(:,2) = conv(target,h_r,'same');


    fsig = (1:length(h_l))./length(h_l).*fs;
    both_fft = fft(h_l);
    % subplot(222);plot(fsig,abs(both_fft))
    % xlim([0 fs/2])

    %2))) noise
    Index = (Elevation - 1)*72+noise_index;%
    h_l = squeeze(out_hart(:,1,Index)); % Left HRIRs
    h_r = squeeze(out_hart(:,2,Index)); % Right HRIRs
    new_noise(:,1) = conv(noise_reversed,h_l,'same');
    new_noise(:,2) = conv(noise_reversed,h_r,'same');
    Combined_dual = (new_tar+new_noise(1:length(target),:))./2; % /2 is to make it softer
    Combined_mono = sum(Combined_dual,2);
    % sound_level = 20.*log10(rms(Combined_dual(:,1))./20e-6)
    % swer0
    % sound(Combined_mono(1:round(fs*4),:),fs) % play out the original noisy sound
    % pause(4)

    % ======== Processing
    % only do the noise
    ifplot=0;
    out_model=f_get_BB_model(h_l,h_r,ifplot,L_or_R);
    ifplot = 1;
    out=f_segregation(out_model.F_centers,Combined_dual,ifplot,L_or_R,soft_hard,rad,f1,Condition,if_mask);
    % out.freqs(1:200)
    % processed_sound = out.reconstructed;
    % sound(processed_sound(1:round(fs*4),:),fs) % play out the processed sound
    % pause(5)
    % audiowrite(['Spatial_sound\saved_Processed_TargetL_Group' num2str(igroup) '_Cond' num2str(Condition) '.wav'],processed_sound,fs)
    clear new_noise new_tar

    % pause
    if Condition==1
        exportgraphics(f1, 'BB_Figure3_60deg.png', 'Resolution', 600,'ContentType', 'vector')
        % elseif Condition==2
        %     exportgraphics(gcf, 'AA_Figure4A_15deg.png', 'Resolution', 300,'ContentType', 'vector')
    elseif Condition==4
        exportgraphics(f1, 'BB_Figure5_15_and_5deg.png', 'Resolution', 600,'ContentType', 'vector')
    end

end